const http = require('http')
const fs = require('fs')
const url = require('url')
const path = require('path')
let mimeData = require('./mime.json')
console.log(typeof mimeData);
const serv = http.createServer((req, res) => {
    var urlRrs = url.parse(req.url, true)
    let pathname = urlRrs.pathname
    res.setHeader("Content-Type", "text/html;charest=utf-8");
    if (pathname == "/index") {
        let data = fs.readFileSync("./views/index.html");
        res.write(data);
        res.end();
    } else if (pathname == "/product") {
        let data = fs.readFileSync("./views/product.html");
        res.write(data);
        res.end();
    } else {
        // 第三方资源
        // MIME 配置文件
        // console.log("??", req.url)


        if (req.url != "/favicon.ico") {
            let extname = path.extname(req.url);
            console.log(extname);
            let contenttype = mimeData[extname];
            res.setHeader("Content-Type", contenttype);
            let data = fs.readFileSync("./static/" + req.url)
            res.write(data);
            res.end();
        }
    }
})
serv.listen(8989);